<?php

/**
 * Contains a class to define an email to be sent, and the means to send it.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses toArray To easily convert strings to arrays. */
require_once 'function_toarray.php';
 
/**
 * A class to define an email to be sent, and the means to send it.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class Sendmail
{
    /**
     * The list of addresses to send the mail to.
     *
     * @access public
     * @var array
     */
    public $to;
    
    /**
     * The from address.
     *
     * @access public
     * @var string
     */
    public $from;
    
    /**
     * The list of CC addresses to send the mail to.
     *
     * @access public
     * @var array
     */
    public $cc;
    
    /**
     * The list of BCC addresses to send the mail to.
     *
     * @access public
     * @var array
     */
    public $bcc;
    
    /**
     * The subject of the message.
     *
     * @access public
     * @var string
     */
    public $subject;
    
    /**
     * The body of the message.
     *
     * @access public
     * @var string
     */
    public $message;
    
    /**
     * Whether to send the message as HTML or not.
     *
     * @access public
     * @var bool
     */
    public $html;
    
    /**
     * Constructor function.  Populates variables based on parameters.
     *
     * @param mixed  $to      The address(es) to send the message to.
     * @param string $subject The subject line of the message.
     * @param string $message The message to send.
     * @param bool   $html    Whether or not to send the message as HTML.
     * @param mixed  $cc      The address(es) to CC the message to.
     * @param mixed  $bcc     The address(es) to BCC the message to.
     *
     * @access public
     * @return null
     */
    public function __construct($to = null, $subject = null, $message = null,
        $html = false, $cc = null, $bcc = null
    ) {
        toArray($to);
        toArray($cc);
        toArray($bcc);
        $this->to      = $to;
        $this->subject = $subject;
        $this->message = $message;
        $this->html    = $html;
        $this->cc      = $cc;
        $this->bcc     = $bcc;
        $this->from    = RETURN_ADDRESS;
    }
    
    /**
     * Function to convert an array of addresses into a string of addresses.
     *
     * @param array $addresses The array of addresses to convert.
     *
     * @access private
     * @return string  The string of addresses.
     */
    private function _convertAddresses($addresses)
    {
        if (count($addresses) == 0) {
            return '';
        }
        $string = '';
        foreach ($addresses as $address) {
            $string .= $address . ', ';
        }
        return substr($string, 0, strlen($string) - 2);
    }
    
    /**
     * Function to add a to address.
     *
     * @param string $address The address to add.
     *
     * @access public
     * @return null
     */
    public function addTo($address)
    {
        $this->to[] = $address;
    }
    
    /**
     * Function to add a cc address.
     *
     * @param string $address The address to add.
     *
     * @access public
     * @return null
     */
    public function addCC($address)
    {
        $this->cc[] = $address;
    }
    
    /**
     * Function to add a bcc address.
     *
     * @param string $address The address to add.
     *
     * @access public
     * @return null
     */
    public function addBCC($address)
    {
        $this->bcc[] = $address;
    }
    
    /**
     * Function to send the email.
     *
     * @access public
     * @return bool   True on success, false on failure.
     */
    public function send()
    {
        if (count($this->to) == 0
            && count($this->cc) == 0
            && count($this->bcc) == 0
        ) {
            return false;
        }
        if ($this->subject == '' && $this->message == '') {
            return false;
        }
        $to  = $this->_convertAddresses($this->to);
        $cc  = $this->_convertAddresses($this->cc);
        $bcc = $this->_convertAddresses($this->bcc);
        $headers = '';
        if ($this->html) {
            $headers .= 'MIME-Version: 1.0' . "\r\n"
                . 'Content-type: text/html; charset=utf-8' . "\r\n";
        }
        $headers .= 'From: ' . SITENAME . ' <' . RETURN_ADDRESS . '>' . "\r\n";
        if ($cc != '') {
            $headers .= 'Cc: ' . $cc . "\r\n";
        }
        if ($bcc != '') {
            $headers .= 'Bcc: ' . $bcc . "\r\n";
        }
        return mail($to, $this->subject, $this->message, $headers);
    }
}

?>